﻿using System;

using System.Collections;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Reflection;

using Global.Class.Library;
using BusinessLogicLayer;
using BusinessLogicLayer.MetaDataList;
using DataAccessLayer;
using DataTransferObject;
using DataTransferObject.MetaDataList;
using System.Globalization;
using BrightIdeasSoftware;
using Equin.ApplicationFramework;

namespace PresentationLayer
{
    public partial class ListCustomerName : Form, IMergeToolStrip, ICallToolStrip
    {

        #region <Implement Interface>

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public GlobalEnum.TaskID TaskID
        {
            get { return this.listCustomerNameBLL.TaskID; }
        }


        public ToolStrip ChildToolStrip
        {
            get
            {
                return this.toolStripChildForm;
            }
            set
            {
                this.toolStripChildForm = value;
            }
        }





        #region Context toolbar


        public bool IsDirty
        {
            get { return this.listCustomerNameBLL.IsDirty; }
        }

        public bool IsValid
        {
            get { return this.listCustomerNameBLL.IsValid; }
        }

        public bool Closable
        {
            get { return true; }
        }

        public bool Loadable
        {
            get { return true; }
        }

        public bool Newable
        {
            get { return !this.listCustomerNameBLL.ReadOnly; }
        }

        public bool Editable
        {
            get { return !this.listCustomerNameBLL.ReadOnly; }
        }

        public bool Deletable
        {
            get { return this.listCustomerNameBLL.Editable; }
        }

        public bool Importable
        {
            get { return false; }
        }

        public bool Exportable
        {
            get { return false; }
        }

        public bool Verifiable
        {
            get { return this.listCustomerNameBLL.Verifiable; }
        }

        public bool Unverifiable
        {
            get { return this.listCustomerNameBLL.Unverifiable; }
        }

        public bool Printable
        {
            get { return false; }
        }

        public bool Searchable
        {
            get { return true; }
        }

        /// <summary>
        /// Edit Mode: True, Read Mode: False
        /// </summary>
        private bool editableMode;
        private int lastCustomerNameID;
        public bool EditableMode { get { return this.editableMode; } }
        /// <summary>
        /// This reverse of the EditableMode
        /// </summary>
        public bool ReadonlyMode { get { return !this.editableMode; } }

        /// <summary>
        /// Set the editableMode
        /// </summary>
        /// <param name="editableMode"></param>
        private void SetEditableMode(bool editableMode)
        {
            if (this.editableMode != editableMode)
            {
                this.lastCustomerNameID = this.listCustomerNameBLL.CustomerID;
                this.editableMode = editableMode;
                this.NotifyPropertyChanged("EditableMode");
                this.NotifyPropertyChanged("ReadonlyMode");

                this.naviGroupDetails.Expanded = editableMode;
            }
        }


        private void CancelDirty(bool restoreSavedData)
        {
            try
            {
                if (restoreSavedData || this.listCustomerNameBLL.CustomerID <= 0)
                    this.listCustomerNameBLL.Fill(this.lastCustomerNameID);

                this.SetEditableMode(false);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }



        #endregion Context toolbar



        #region ICallToolStrip

        public void Escape()
        {
            if (this.EditableMode)
            {
                if (this.IsDirty)
                {
                    DialogResult dialogResult = MessageBox.Show(this, "Do you want to save your change?", "Warning", MessageBoxButtons.YesNoCancel);
                    if (dialogResult == DialogResult.Yes)
                    {
                        this.Save();
                        if (!this.IsDirty) this.CancelDirty(false);
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        this.CancelDirty(true);
                    }
                    else
                        return;
                }
                else
                    this.CancelDirty(false);
            }
            else
                this.Close(); //Unload this module
        }


        public void Loading()
        {
            this.GetMasterList();
        }

        public void New()
        {
            this.listCustomerNameBLL.New();
            this.SetEditableMode(true);
        }

        public void Edit()
        {
            this.SetEditableMode(true);
        }

        public void Save()
        {
            this.listCustomerNameBLL.Save();
            this.GetMasterList();
        }

        public void Delete()
        {
            DialogResult dialogResult = MessageBox.Show(this, "Are you sure you want to delete " + this.listCustomerNameBLL.ListCustomerNameMaster.Description + "?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (dialogResult == DialogResult.Yes)
            {
                try
                {
                    this.listCustomerNameBLL.Delete();
                    this.GetMasterList();
                }
                catch (Exception exception)
                {
                    GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
                }
            }
        }

        public void Import()
        {
            ImportExcel();
        }

        public void Export()
        {
            try
            {
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        public void Verify()
        {
            MessageBox.Show("Verify");
        }

        public void Print(PrintDestination printDestination)
        {
            MessageBox.Show("Print");
        }

        public void SearchText(string searchText)
        {
            CommonFormAction.OLVFilter(this.dataTreeListViewMaster, searchText);
        }

        #endregion


        #endregion <Implement Interface>








        #region <Storage>
        private ListCustomerNameBLL listCustomerNameBLL;

        private int filterAddressAreaID = -1;
        private int filterCustomerCategoryID = -1;
        private int filterCustomerTypeID = -1;
        private int filterCustomerChannelID = -1;
        private int filterSalesmenID = -1;

        [DefaultValue(0)]
        public int FilterAddressAreaID
        {
            get { return this.filterAddressAreaID; }
            set
            {
                if (this.filterAddressAreaID != value)
                {
                    this.filterAddressAreaID = value;
                    this.GetMasterList();//this.NotifyPropertyChanged("FilterAddressAreaID");
                }
            }
        }

        [DefaultValue(0)]
        public int FilterCustomerCategoryID
        {
            get { return this.filterCustomerCategoryID; }
            set
            {
                if (this.filterCustomerCategoryID != value)
                {
                    this.filterCustomerCategoryID = value;
                    this.GetMasterList();//this.NotifyPropertyChanged("FilterCustomerCategoryID");
                }
            }
        }

        [DefaultValue(0)]
        public int FilterCustomerTypeID
        {
            get { return this.filterCustomerTypeID; }
            set
            {
                if (this.filterCustomerTypeID != value)
                {
                    this.filterCustomerTypeID = value;
                    this.GetMasterList();//this.NotifyPropertyChanged("FilterCustomerTypeID");
                }
            }
        }

        [DefaultValue(0)]
        public int FilterCustomerChannelID
        {
            get { return this.filterCustomerChannelID; }
            set
            {
                if (this.filterCustomerChannelID != value)
                {
                    this.filterCustomerChannelID = value;
                    this.GetMasterList();//this.NotifyPropertyChanged("FilterCustomerChannelID");
                }
            }
        }

        [DefaultValue(0)]
        public int FilterSalesmenID
        {
            get { return this.filterSalesmenID; }
            set
            {
                if (this.filterSalesmenID != value)
                {
                    this.filterSalesmenID = value;
                    this.GetMasterList();//this.NotifyPropertyChanged("FilterSalesmenID");
                }
            }
        }

        #endregion <Storage>


        public ListCustomerName()
        {
            try
            {
                InitializeComponent();

                this.listCustomerNameBLL = new ListCustomerNameBLL();

                this.listCustomerNameBLL.PropertyChanged += new PropertyChangedEventHandler(listCustomerNameBLL_PropertyChanged);

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        void listCustomerNameBLL_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.NotifyPropertyChanged(e.PropertyName);
        }


        private void ListCustomerName_Load(object sender, EventArgs e)
        {
            try
            {
                InitializeTabControl();

                InitializeCommonControlBinding();

                InitializeDataGridBinding();

                InitializeReadOnlyModeBinding();
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        private void InitializeTabControl()
        {
            try
            {
                CustomTabControl customTabControlCustomerSalesmen = new CustomTabControl();

                customTabControlCustomerSalesmen.ImageList = this.imageListTabControl;


                customTabControlCustomerSalesmen.TabPages.Add("CustomerSalesmen", "Salesmen    ", 2);
                customTabControlCustomerSalesmen.TabPages[0].Controls.Add(this.dataGridViewListCustomerNameSalesmen);
                this.dataGridViewListCustomerNameSalesmen.Dock = DockStyle.Fill;


                customTabControlCustomerSalesmen.DisplayStyle = TabStyle.VisualStudio;
                customTabControlCustomerSalesmen.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
                this.naviGroupDetails.Controls.Add(customTabControlCustomerSalesmen);
                this.naviGroupDetails.Controls.SetChildIndex(customTabControlCustomerSalesmen, 0);
                customTabControlCustomerSalesmen.Dock = DockStyle.Fill;


            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        #region <InitializeBinding>


        Binding customerCategoryIDBinding;
        Binding customerTypeIDBinding;
        Binding salesmenIDBinding;
        Binding addressAreaIDBinding;
        Binding customerGroupIDBinding;
        Binding customerChannelIDBinding;
        Binding masterCustomerIDBinding;


        Binding filterAddressAreaIDBinding;
        Binding filterCustomerCategoryIDBinding;
        Binding filterCustomerTypeIDBinding;
        Binding filterCustomerChannelIDBinding;
        Binding filterSalesmenIDBinding;


        Binding descriptionBinding;
        Binding descriptionOfficialBinding;
        Binding descriptionOriginalBinding;
        Binding vatCodeBinding;
        Binding contactNameBinding;
        Binding attentionNameBinding;
        Binding telephoneBinding;
        Binding emailAddressBinding;
        Binding addressNoBinding;
        Binding otherBinding;


        Binding isMasterCustomerBinding;
        Binding isTenderCustomerBinding;


        Binding isDirtyBinding;
        Binding isDirtyBLLBinding;

        private void InitializeCommonControlBinding()
        {
            CommonMetaList commonMetaList = new CommonMetaList();


            ERmgrUIP.ListCustomerCategoryDataTable listItemCustomerCategoryDataTable = commonMetaList.GetCustomerCategory();
            this.comboBoxCustomerCategoryID.DataSource = listItemCustomerCategoryDataTable;
            this.comboBoxCustomerCategoryID.DisplayMember = listItemCustomerCategoryDataTable.DescriptionColumn.ColumnName;
            this.comboBoxCustomerCategoryID.ValueMember = listItemCustomerCategoryDataTable.CustomerCategoryIDColumn.ColumnName;
            this.customerCategoryIDBinding = this.comboBoxCustomerCategoryID.DataBindings.Add("SelectedValue", this.listCustomerNameBLL.ListCustomerNameMaster, "CustomerCategoryID", true);

            this.toolStripComboBoxCustomerCategoryID.ComboBox.DataSource = listItemCustomerCategoryDataTable.Copy();
            this.toolStripComboBoxCustomerCategoryID.ComboBox.DisplayMember = listItemCustomerCategoryDataTable.DescriptionColumn.ColumnName;
            this.toolStripComboBoxCustomerCategoryID.ComboBox.ValueMember = listItemCustomerCategoryDataTable.CustomerCategoryIDColumn.ColumnName;
            this.filterCustomerCategoryIDBinding = this.toolStripComboBoxCustomerCategoryID.ComboBox.DataBindings.Add("SelectedValue", this, "FilterCustomerCategoryID", true);


            ERmgrUIP.ListCustomerTypeDataTable listItemCustomerTypeDataTable = commonMetaList.GetCustomerType();
            this.comboBoxCustomerTypeID.DataSource = listItemCustomerTypeDataTable;
            this.comboBoxCustomerTypeID.DisplayMember = listItemCustomerTypeDataTable.DescriptionColumn.ColumnName;
            this.comboBoxCustomerTypeID.ValueMember = listItemCustomerTypeDataTable.CustomerTypeIDColumn.ColumnName;
            this.customerTypeIDBinding = this.comboBoxCustomerTypeID.DataBindings.Add("SelectedValue", this.listCustomerNameBLL.ListCustomerNameMaster, "CustomerTypeID", true);

            this.toolStripComboBoxCustomerTypeID.ComboBox.DataSource = listItemCustomerTypeDataTable.Copy();
            this.toolStripComboBoxCustomerTypeID.ComboBox.DisplayMember = listItemCustomerTypeDataTable.DescriptionColumn.ColumnName;
            this.toolStripComboBoxCustomerTypeID.ComboBox.ValueMember = listItemCustomerTypeDataTable.CustomerTypeIDColumn.ColumnName;
            this.filterCustomerTypeIDBinding = this.toolStripComboBoxCustomerTypeID.ComboBox.DataBindings.Add("SelectedValue", this, "FilterCustomerTypeID", true);


            ERmgrUIP.ListSalesmenNameDataTable listItemSalesmenNameDataTable = commonMetaList.GetSalesmenName();
            this.comboBoxSalesmenID.DataSource = listItemSalesmenNameDataTable;
            this.comboBoxSalesmenID.DisplayMember = listItemSalesmenNameDataTable.DescriptionOfficialColumn.ColumnName;
            this.comboBoxSalesmenID.ValueMember = listItemSalesmenNameDataTable.SalesmenIDColumn.ColumnName;
            this.salesmenIDBinding = this.comboBoxSalesmenID.DataBindings.Add("SelectedValue", this.listCustomerNameBLL.ListCustomerNameMaster, "SalesmenID", true);

            listItemSalesmenNameDataTable = (ERmgrUIP.ListSalesmenNameDataTable)listItemSalesmenNameDataTable.Copy();
            listItemSalesmenNameDataTable.AddListSalesmenNameRow("[All  salesmen]", "[All salesmen]", 0, 0, 0);
            this.toolStripComboBoxSalesmenID.ComboBox.DataSource = listItemSalesmenNameDataTable;
            this.toolStripComboBoxSalesmenID.ComboBox.DisplayMember = listItemSalesmenNameDataTable.DescriptionColumn.ColumnName;
            this.toolStripComboBoxSalesmenID.ComboBox.ValueMember = listItemSalesmenNameDataTable.SalesmenIDColumn.ColumnName;
            this.filterSalesmenIDBinding = this.toolStripComboBoxSalesmenID.ComboBox.DataBindings.Add("SelectedValue", this, "FilterSalesmenID", true);



            ERmgrUIP.ListAddressAreaDataTable listItemAddressAreaDataTable = commonMetaList.GetAddressArea();
            this.comboBoxAddressAreaID.DataSource = listItemAddressAreaDataTable;
            this.comboBoxAddressAreaID.DisplayMember = listItemAddressAreaDataTable.DescriptionColumn.ColumnName;
            this.comboBoxAddressAreaID.ValueMember = listItemAddressAreaDataTable.AddressAreaIDColumn.ColumnName;
            this.addressAreaIDBinding = this.comboBoxAddressAreaID.DataBindings.Add("SelectedValue", this.listCustomerNameBLL.ListCustomerNameMaster, "AddressAreaID", true);

            this.toolStripComboBoxAddressAreaID.ComboBox.DataSource = listItemAddressAreaDataTable.Copy();
            this.toolStripComboBoxAddressAreaID.ComboBox.DisplayMember = listItemAddressAreaDataTable.DescriptionColumn.ColumnName;
            this.toolStripComboBoxAddressAreaID.ComboBox.ValueMember = listItemAddressAreaDataTable.AddressAreaIDColumn.ColumnName;
            this.filterAddressAreaIDBinding = this.toolStripComboBoxAddressAreaID.ComboBox.DataBindings.Add("SelectedValue", this, "FilterAddressAreaID", true);



            ERmgrUIP.ListCustomerGroupDataTable listItemCustomerGroupDataTable = commonMetaList.GetCustomerGroup();
            this.comboBoxCustomerGroupID.DataSource = listItemCustomerGroupDataTable;
            this.comboBoxCustomerGroupID.DisplayMember = listItemCustomerGroupDataTable.DescriptionColumn.ColumnName;
            this.comboBoxCustomerGroupID.ValueMember = listItemCustomerGroupDataTable.CustomerGroupIDColumn.ColumnName;
            this.customerGroupIDBinding = this.comboBoxCustomerGroupID.DataBindings.Add("SelectedValue", this.listCustomerNameBLL.ListCustomerNameMaster, "CustomerGroupID", true);




            ERmgrUIP.ListCustomerChannelDataTable listItemCustomerChannelDataTable = commonMetaList.GetCustomerChannel();
            this.comboBoxCustomerChannelID.DataSource = listItemCustomerChannelDataTable;
            this.comboBoxCustomerChannelID.DisplayMember = listItemCustomerChannelDataTable.DescriptionColumn.ColumnName;
            this.comboBoxCustomerChannelID.ValueMember = listItemCustomerChannelDataTable.CustomerChannelIDColumn.ColumnName;
            this.customerChannelIDBinding = this.comboBoxCustomerChannelID.DataBindings.Add("SelectedValue", this.listCustomerNameBLL.ListCustomerNameMaster, "CustomerChannelID", true);

            this.toolStripComboBoxCustomerChannelID.ComboBox.DataSource = listItemCustomerChannelDataTable.Copy();
            this.toolStripComboBoxCustomerChannelID.ComboBox.DisplayMember = listItemCustomerChannelDataTable.DescriptionColumn.ColumnName;
            this.toolStripComboBoxCustomerChannelID.ComboBox.ValueMember = listItemCustomerChannelDataTable.CustomerChannelIDColumn.ColumnName;
            this.filterCustomerChannelIDBinding = this.toolStripComboBoxCustomerChannelID.ComboBox.DataBindings.Add("SelectedValue", this, "FilterCustomerChannelID", true);


            this.descriptionBinding = this.textBoxDescription.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "Description", true);
            this.descriptionOfficialBinding = this.textBoxDescriptionOfficial.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "DescriptionOfficial", true);
            this.addressNoBinding = this.textBoxAddressNo.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "AddressNo", true);
            this.vatCodeBinding = this.textBoxVATCode.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "VATCode", true);
            this.contactNameBinding = this.textBoxContactName.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "ContactName", true);
            this.attentionNameBinding = this.textBoxAttentionName.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "AttentionName", true);
            this.telephoneBinding = this.textBoxTelephone.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "Telephone", true);
            this.emailAddressBinding = this.textBoxEMailAddress.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "EMailAddress", true);
            this.otherBinding = this.textBoxOther.DataBindings.Add("Text", this.listCustomerNameBLL.ListCustomerNameMaster, "Other", true);

            this.isMasterCustomerBinding = this.checkBoxIsMasterCustomer.DataBindings.Add("Checked", this.listCustomerNameBLL.ListCustomerNameMaster, "IsMasterCustomer", true);
            this.isTenderCustomerBinding = this.checkBoxIsTenderCustomer.DataBindings.Add("Checked", this.listCustomerNameBLL.ListCustomerNameMaster, "IsTenderCustomer", true);


            this.isDirtyBinding = this.checkBoxIsDirty.DataBindings.Add("Checked", this.listCustomerNameBLL.ListCustomerNameMaster, "IsDirty", true);
            this.isDirtyBLLBinding = this.checkBoxIsDirtyBLL.DataBindings.Add("Checked", this.listCustomerNameBLL, "IsDirty", true);



            this.customerCategoryIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.customerTypeIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.salesmenIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.addressAreaIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.customerGroupIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.customerChannelIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


            this.filterAddressAreaIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.filterCustomerCategoryIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.filterCustomerTypeIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.filterCustomerChannelIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.filterSalesmenIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


            this.descriptionBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.descriptionOfficialBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.vatCodeBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.contactNameBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.attentionNameBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.telephoneBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.emailAddressBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.addressNoBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.otherBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


            this.isMasterCustomerBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.isTenderCustomerBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


            this.isDirtyBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.isDirtyBLLBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


            this.naviGroupDetails.DataBindings.Add("ExpandedHeight", this.numericUpDownSizingDetail, "Value", true, DataSourceUpdateMode.OnPropertyChanged);
            this.numericUpDownSizingDetail.Minimum = this.naviGroupDetails.HeaderHeight * 2;
            this.numericUpDownSizingDetail.Maximum = this.naviGroupDetails.Height + this.dataTreeListViewMaster.Height;

            this.tableLayoutPanelMaster.ColumnStyles[this.tableLayoutPanelMaster.ColumnCount - 1].SizeType = SizeType.Absolute; this.tableLayoutPanelMaster.ColumnStyles[this.tableLayoutPanelMaster.ColumnCount - 1].Width = 10;
            this.tableLayoutPanelExtend.ColumnStyles[this.tableLayoutPanelExtend.ColumnCount - 1].SizeType = SizeType.Absolute; this.tableLayoutPanelExtend.ColumnStyles[this.tableLayoutPanelExtend.ColumnCount - 1].Width = 10;

            this.errorProviderMaster.DataSource = this.listCustomerNameBLL.ListCustomerNameMaster;

        }


        private void CommonControl_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState == BindingCompleteState.Exception) { GlobalExceptionHandler.ShowExceptionMessageBox(this, e.ErrorText); e.Cancel = true; }
        }

        private void numericUpDownSizingDetail_ValueChanged(object sender, EventArgs e)
        {
            this.naviGroupDetails.Expand();
        }

        private void naviGroupDetails_HeaderMouseClick(object sender, MouseEventArgs e)
        {
            this.numericUpDownSizingDetail.Visible = this.naviGroupDetails.Expanded;
            this.toolStripNaviGroupDetails.Visible = this.naviGroupDetails.Expanded;
        }


        private void toolStripButtonShowDetailsExtend_Click(object sender, EventArgs e)
        {
            this.naviGroupDetailsExtend.Expanded = !this.naviGroupDetailsExtend.Expanded;
            this.toolStripButtonShowDetailsExtend.Image = this.naviGroupDetailsExtend.Expanded ? ResourceIcon.Chevron_Collapse.ToBitmap() : ResourceIcon.Chevron_Expand.ToBitmap();
        }

        private void InitializeDataGridBinding()
        {

            #region Master list
            Dictionary<int, string> dictionary = new Dictionary<int, string>(); //View by Filter
            foreach (int enumValue in Enum.GetValues(typeof(ListingOptions)))
                dictionary.Add(enumValue, this.GetEnumDescription((ListingOptions)enumValue));

            this.toolStripComboBoxListingOptions.ComboBox.DataSource = new BindingSource(dictionary, null); ;
            this.toolStripComboBoxListingOptions.ComboBox.DisplayMember = "Value";
            this.toolStripComboBoxListingOptions.ComboBox.ValueMember = "Key";
            #endregion Master list


            #region <dataGridViewDetail>


            this.dataGridViewListCustomerNameSalesmen.AutoGenerateColumns = false;
            listCustomerNameSalesmenView = new BindingListView<ListCustomerNameSalesmen>(this.listCustomerNameBLL.ListCustomerNameSalesmenList);
            this.dataGridViewListCustomerNameSalesmen.DataSource = listCustomerNameSalesmenView;

            //StackedHeaderDecorator stackedHeaderDecorator1 = new StackedHeaderDecorator(this.dataGridViewListCustomerNameSalesmen);

            DataGridViewComboBoxColumn comboBoxColumn;
            CommonMetaList commonMetaList = new CommonMetaList();

            ERmgrUIP.ListSalesmenNameDataTable listItemSalesmenNameDataTable = commonMetaList.GetSalesmenName(true);
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCustomerNameSalesmen.Columns[listItemSalesmenNameDataTable.SalesmenIDColumn.ColumnName];
            comboBoxColumn.DataSource = listItemSalesmenNameDataTable;
            comboBoxColumn.DisplayMember = listItemSalesmenNameDataTable.DescriptionColumn.ColumnName;
            comboBoxColumn.ValueMember = listItemSalesmenNameDataTable.SalesmenIDColumn.ColumnName;

            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCustomerNameSalesmen.Columns["SalesmenName"];
            comboBoxColumn.DataSource = listItemSalesmenNameDataTable;
            comboBoxColumn.DisplayMember = listItemSalesmenNameDataTable.DescriptionOfficialColumn.ColumnName;
            comboBoxColumn.ValueMember = listItemSalesmenNameDataTable.SalesmenIDColumn.ColumnName;

            #endregion <dataGridViewDetail>
        }


        private void InitializeReadOnlyModeBinding()
        {
            List<Control> controlList = GlobalStaticFunction.GetAllControls(this);

            foreach (Control control in controlList)
            {
                if (this.toolStripChildForm.Contains(control))
                    control.DataBindings.Add("Enabled", this, "ReadonlyMode");
                else
                {
                    //if (control is TextBox) control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                    if (control is TextBox)
                    {
                        if (control.Equals(this.textBoxDescription))
                            control.Enabled = false;
                        else
                            control.DataBindings.Add("Enabled", this, "EditableMode");
                    }
                    else if (control is ComboBox || control is DateTimePicker || control is CheckBox) control.DataBindings.Add("Enabled", this, "EditableMode");
                    else if (control is DataGridView)
                    {
                        control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                        control.DataBindings.Add("AllowUserToAddRows", this, "EditableMode");
                        control.DataBindings.Add("AllowUserToDeleteRows", this, "EditableMode");
                    }
                }
            }

            this.dataTreeListViewMaster.DataBindings.Add("Enabled", this, "ReadonlyMode");
        }

        BindingListView<ListCustomerNameSalesmen> listCustomerNameSalesmenView;


        private enum ListingOptions
        {
            [Description("Territory")]
            AddressArea = 1,
            [Description("Customer Class")]
            CustomerCategory = 2,
            [Description("Customer Channel")]
            CustomerType = 3,
            [Description("Customer Channel 2")]
            CustomerChannel = 4,
            [Description("Master Customer Only")]
            MasterCustomer = 5,
            [Description("Tender Customerm Only")]
            TenderCustomer = 6
        }

        private string GetEnumDescription(Enum enumValue)
        {
            FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString());

            DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)
                return attributes[0].Description;
            else
                return enumValue.ToString();
        }

        private void GetMasterList()
        {
            try
            {
                if (this.toolStripComboBoxListingOptions.ComboBox.SelectedItem == null) return;

                DataTable listCustomerNameListingDataTable;

                KeyValuePair<int, string> keyValuePair = (KeyValuePair<int, string>)this.toolStripComboBoxListingOptions.ComboBox.SelectedItem;

                switch ((ListingOptions)keyValuePair.Key)
                {
                    case ListingOptions.AddressArea:
                        if (this.FilterAddressAreaID < 0)
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameEmptyListing();
                        else
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameAddressAreaListing(this.FilterAddressAreaID, this.FilterSalesmenID);
                        break;
                    case ListingOptions.CustomerCategory:
                        if (this.FilterCustomerCategoryID < 0)
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameEmptyListing();
                        else
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameCustomerCategoryListing(this.FilterCustomerCategoryID, this.FilterSalesmenID);
                        break;
                    case ListingOptions.CustomerChannel:
                        if (this.FilterCustomerChannelID < 0)
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameEmptyListing();
                        else
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameCustomerChannelListing(this.FilterCustomerChannelID, this.FilterSalesmenID);
                        break;
                    case ListingOptions.CustomerType:
                        if (this.FilterCustomerTypeID < 0)
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameEmptyListing();
                        else
                            listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameCustomerTypeListing(this.FilterCustomerTypeID, this.FilterSalesmenID);
                        break;
                    case ListingOptions.MasterCustomer:
                        listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameMasterCustomerListing(this.FilterSalesmenID);
                        break;
                    case ListingOptions.TenderCustomer:
                        listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameTenderCustomerListing(this.FilterSalesmenID);
                        break;
                    default:
                        listCustomerNameListingDataTable = this.listCustomerNameBLL.CustomerNameEmptyListing();
                        break;
                }

                listCustomerNameListingDataTable.TableName = "ListCustomerNameListingDataTable";

                DataSet bindingDataSet = new DataSet();
                bindingDataSet.Tables.Add(listCustomerNameListingDataTable);

                this.dataTreeListViewMaster.FullRowSelect = true;
                this.dataTreeListViewMaster.KeyAspectName = "CustomerID";
                this.dataTreeListViewMaster.ParentKeyAspectName = "AncestorID";
                this.dataTreeListViewMaster.RootKeyValue = 0;

                this.dataTreeListViewMaster.DataMember = "ListCustomerNameListingDataTable";
                this.dataTreeListViewMaster.DataSource = new DataViewManager(bindingDataSet);

                this.dataTreeListViewMaster.ExpandAll();
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        #endregion <InitializeBinding>





        #region Import Excel

        private void ImportExcel()
        {
            try
            {

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }

        }
        #endregion Import Excel



        private void dataTreeListViewMaster_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ObjectListView objectListView = (ObjectListView)sender;
                DataRowView row = (DataRowView)objectListView.SelectedObject;

                if (row != null)
                {
                    bool isCustomer; int customerTypeID;

                    if (bool.TryParse(row.Row["IsCustomer"].ToString(), out isCustomer))
                        if (isCustomer && int.TryParse(row.Row["CustomerID"].ToString(), out customerTypeID)) this.listCustomerNameBLL.Fill(customerTypeID);
                        else this.listCustomerNameBLL.Fill(0);
                }
                else this.listCustomerNameBLL.Fill(0);
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        private void dataTreeListViewMaster_DoubleClick(object sender, EventArgs e)
        {
            this.naviGroupDetails.Expanded = true;
        }

        private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            GlobalExceptionHandler.ShowExceptionMessageBox(this, e.Exception);
        }

        private void toolStripComboBoxListingOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                KeyValuePair<int, string> keyValuePair = (KeyValuePair<int, string>)this.toolStripComboBoxListingOptions.ComboBox.SelectedItem;

                this.toolStripComboBoxAddressAreaID.Visible = (ListingOptions)keyValuePair.Key == ListingOptions.AddressArea;
                this.toolStripComboBoxCustomerCategoryID.Visible = (ListingOptions)keyValuePair.Key == ListingOptions.CustomerCategory;
                this.toolStripComboBoxCustomerChannelID.Visible = (ListingOptions)keyValuePair.Key == ListingOptions.CustomerChannel;
                this.toolStripComboBoxCustomerTypeID.Visible = (ListingOptions)keyValuePair.Key == ListingOptions.CustomerType;

                this.toolStripLabelFilter.Visible = (ListingOptions)keyValuePair.Key == ListingOptions.AddressArea || (ListingOptions)keyValuePair.Key == ListingOptions.CustomerCategory || (ListingOptions)keyValuePair.Key == ListingOptions.CustomerChannel || (ListingOptions)keyValuePair.Key == ListingOptions.CustomerType;

                this.GetMasterList();
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        private void toolStripButtonTrackingAndDependent_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult mainResult;
                if (sender.Equals(this.toolStripButtonTracking))
                {
                    ListCustomerNameTracking listCustomerNameTracking = new ListCustomerNameTracking(this.listCustomerNameBLL.CustomerID, this.listCustomerNameBLL.ListCustomerNameMaster.DescriptionOfficial);
                    mainResult = listCustomerNameTracking.ShowDialog();
                }
                else
                {
                    ListCustomerNameDependent listCustomerNameDependent = new ListCustomerNameDependent(this.listCustomerNameBLL.CustomerID, this.listCustomerNameBLL.ListCustomerNameMaster.DescriptionOfficial);
                    mainResult = listCustomerNameDependent.ShowDialog();
                }

                if (mainResult == DialogResult.OK)
                {
                    if (sender.Equals(this.toolStripButtonTracking)) this.listCustomerNameBLL.Fill(this.listCustomerNameBLL.CustomerID);

                    DialogResult dialogResult = MessageBox.Show(this, "Do you want to reload the customer list?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (dialogResult == DialogResult.Yes) this.Loading();
                }
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        private void toolStripButtonFindAndCombine_Click(object sender, EventArgs e)
        {
            try
            {
                DialogFindAndCombine listCustomerNameDependent = new DialogFindAndCombine("ListCustomerName", "CustomerID", "Description + '-' + DescriptionOfficial", "AddressNo", "Description", this.listCustomerNameBLL.CustomerID);
                if (listCustomerNameDependent.ShowDialog() == DialogResult.OK)
                {
                    this.listCustomerNameBLL.Fill(this.listCustomerNameBLL.CustomerID);

                    DialogResult dialogResult = MessageBox.Show(this, "Do you want to reload the customer list?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (dialogResult == DialogResult.Yes) this.Loading();
                }
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }



    }




}
